<template>
  <el-drawer v-model="drawerVisible" :destroy-on-close="true" size="450px" :title="`${drawerProps.title}管理员`">
    <el-form
      ref="ruleFormRef"
      label-width="100px"
      label-suffix=" :"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="drawerProps.row"
      :hide-required-asterisk="drawerProps.isView"
    >
      <el-form-item label="管理员头像" prop="avatar">
        <UploadImg v-model:image-url="drawerProps.row!.avatar" width="135px" height="135px" :file-size="3">
          <template #empty>
            <el-icon><Avatar /></el-icon>
            <span>请上传头像</span>
          </template>
          <template #tip> 头像大小不能超过 3M </template>
        </UploadImg>
      </el-form-item>
      <el-form-item label="管理昵称" prop="nickname">
        <el-input v-model="drawerProps.row!.nickname" placeholder="请填写用户姓名" clearable></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-select v-model="drawerProps.row!.gender" placeholder="请选择性别" clearable>
          <el-option v-for="item in genderType" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="号码" prop="phone">
        <el-input v-model="drawerProps.row!.phone" placeholder="请填写号码" clearable></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="drawerProps.row!.email" placeholder="请填写邮箱" clearable></el-input>
      </el-form-item>
      <el-form-item label="是否为超级管理员" prop="isSuper">
        <el-select v-model="drawerProps.row!.isSuper" placeholder="请选择" clearable>
          <el-option v-for="item in Role" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="drawerVisible = false">取消</el-button>
      <el-button v-show="!drawerProps.isView" type="primary" @click="handleSubmit">确定</el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts" name="UserDrawer">
import { ref, reactive } from "vue";
import { genderType, Role } from "@/utils/dict";
import { ElMessage, FormInstance } from "element-plus";
import { Admin, User } from "@/api/interface";
import UploadImg from "@/components/Upload/Img.vue";

interface DrawerProps {
  title: string;
  isView: boolean;
  row: Partial<Admin.ResAdmin>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  row: {}
});
const rules = reactive({
  phone: [{ required: true, message: "请输入号码", trigger: "blur" }],
  isSuper: [{ required: true, message: "请输入号码", trigger: "blur" }]
});
// 接收父组件传过来的参数
const acceptParams = (params: DrawerProps) => {
  console.log("parmas", params);
  drawerProps.value = params;
  drawerVisible.value = true;
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    const parmas = {
      admin: {
        phone: drawerProps.value.row.phone,
        isSuper: drawerProps.value.row.isSuper
      },
      adminInfo: {
        nickname: drawerProps.value.row.nickname,
        avatar: drawerProps.value.row.avatar,
        gender: drawerProps.value.row.gender,
        email: drawerProps.value.row.email
      }
    };
    console.log("adminInfo", parmas);
    try {
      await drawerProps.value.api!(parmas);
      ElMessage.success({ message: `${drawerProps.value.title}成功！` });
      drawerProps.value.getTableList!();
      drawerVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  acceptParams
});
</script>
